package com.zbs.framework.core.userfunction.action;

import com.alibaba.fastjson.JSONArray;
import com.zbs.framework.common.utils.ResultData;
import com.zbs.framework.common.utils.ResultUtil;
import com.zbs.framework.dao.rolefunction.service.UserFunctionService;
import com.zbs.framework.dao.rolefunction.service.model.UserFunctionInfo;
import com.zbs.framework.dao.userfunction.view.UserFunctionInfoView;
import com.zbs.framework.security.utils.ThreadLocalClient;
import com.zbs.framework.swagger.annotations.ApiOperation;
import com.zbs.framework.swagger.annotations.ApiParam;
import com.zbs.framework.swagger.annotations.ApiTags;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Create by Zbs on 2019/04/30.
 */
@ApiTags(value = "用户功能管理", group = "UIM")
@RestController
@RequestMapping("/userFunction")
public class UserFunctionController {

    @Autowired
    private UserFunctionService userFunctionService;

    /**
     * 编辑用户功能
     * @param userId
     *          用户ID
     * @param functionIds
     *          功能ID json字符串
     * @return
     */
    @ApiOperation("编辑用户功能")
    @PostMapping("/edit")
    @PreAuthorize("hasAuthority('sys::editUserFunction')")
    public ResultData<?> edit(@ApiParam("用户id") Long userId,@ApiParam("功能id JSON字符串") String functionIds) {
        List<Long> ids = JSONArray.parseArray(functionIds, Long.class);
        userFunctionService.editUserFunctions(userId, ids);
        return ResultUtil.ok();
    }

    /**
     * 获取用户拥有的功能ID数组
     * @param userId
     *          用户ID
     * @return
     */
    @ApiOperation(value = "获取用户拥有的功能ID数组",response = Long.class)
    @PostMapping("/getFuncIds")
    @PreAuthorize("hasAuthority('sys::getUserFuncIds')")
    public ResultData<List<Long>> getFuncIds(@ApiParam("用户id")  Long userId) {
        return ResultUtil.ok(userFunctionService.getFuncIds(userId));
    }

    /**
     * 获取用户拥有的所有功能信息
     *
     * @param userId
     *          用户ID
     * @return
     */
    @ApiOperation(value = "获取用户拥有的所有功能信息",response = UserFunctionInfoView.class)
    @PostMapping("/getUserAllFunctions")
    @PreAuthorize("hasAuthority('sys::getUserAllFuncs')")
    public ResultData<List<UserFunctionInfo>> getUserAllFunctions(@ApiParam("用户id") Long userId) {
        return ResultUtil.ok(userFunctionService.getUserAllFunctions(userId));
    }

    /**
     * 获取当前登录用户拥有的功能ID数组
     *
     * @return
     */
    @ApiOperation(value = "获取当前登录用户拥有的功能ID数组",response = Long.class)
    @PostMapping("/getSelfFuncIds")
    public ResultData<List<Long>> getSelfFuncIds() {
        Long userId = ThreadLocalClient.getUserId();
        return ResultUtil.ok(userFunctionService.getFuncIds(userId));
    }

    /**
     * 获取当前登录用户拥有的所有功能
     *
     * @return
     */
    @ApiOperation(value = "获取当前登录用户拥有的所有功能",response = UserFunctionInfoView.class)
    @PostMapping("/getSelfAllFunctions")
    public ResultData<List<UserFunctionInfo>> getSelfAllFunctions() {
        Long userId = ThreadLocalClient.getUserId();
        return ResultUtil.ok(userFunctionService.getUserAllFunctions(userId));
    }
}
